package mail

import (
	"context"
	"github.com/gogf/gf/v2/os/gctx"
	"time"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/event"
	"xr-game-server/core/httpserver"
	"xr-game-server/core/xrpool"
	"xr-game-server/dao/maildao"
	"xr-game-server/dto/maildto"
	"xr-game-server/entity"
	"xr-game-server/errercode"
	"xr-game-server/gameevent"
)

const (
	Max = 200
)

var sysMail []*entity.SysMail

func initRoleMail() {
	xrpool.AddWithRecover(gctx.New(), func(ctx context.Context) {
		sysMail = maildao.GetAllSysMail()
	})
}

// 检查系统邮件,发行没有就加
func chkAndAddSysMail(roleId uint64) {
	for _, val := range sysMail {
		roleSysMail := maildao.GetRoleSysMail(roleId, val.ID)
		if roleSysMail.IsDeleted {
			//添加系统邮件
			roleMail := entity.NewRoleMailForSysMail(roleId, val.ID, val.Type)
			maildao.AddRoleMail(roleMail)
			roleSysMail.SetIsDeleted(false)
		}
	}
	//开始清理超多邮件
	roleMail := maildao.GetRoleMailBy(roleId)
	if len(roleMail) < Max {
		return
	}
	//删除max值后面的邮件
	current := len(roleMail)
	for i := Max; i < current; i++ {
		roleMail[i].SetIsDeleted(true)
	}
	roleMail = roleMail[:Max]
	maildao.FlushRoleMail(roleId, roleMail)
}

func GetRoleMail(ctx context.Context, getRoleMailReq *maildto.GetRoleMailReq) ([]*maildto.RoleMailDto, error) {
	roleId := httpserver.GetAuthId(ctx)
	chkAndAddSysMail(roleId)
	roleMails := maildao.GetRoleMailBy(roleId)
	res := make([]*maildto.RoleMailDto, common.Zero)
	for _, val := range roleMails {
		attachments := maildao.GetAttachment(val.ID)
		dto := maildto.NewRoleMailDto(val, attachments)
		res = append(res, dto)
	}
	return res, nil
}
func SetMailRead(ctx context.Context, setMailReadReq *maildto.SetReadMailReq) (res bool, er error) {
	roleId := httpserver.GetAuthId(ctx)
	roleMail := maildao.GetRoleMailBy(roleId)
	for _, mail := range roleMail {
		if mail.ID == setMailReadReq.MailId {
			if mail.IsRead {
				return false, errercode.CreateCode(errercode.MailIsRead)
			}
			mail.SetIsRead(true)
		}
	}
	return true, nil
}
func ReceiveMail(ctx context.Context, receiveMailReq *maildto.ReceiveMailReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	roleMail := maildao.GetRoleMailBy(roleId)
	for _, mail := range roleMail {
		if mail.ID == receiveMailReq.MailId {
			if mail.IsReceived {
				return false, errercode.CreateCode(errercode.MailIsReceived)
			}
			mail.SetIsReceived(true)
			now := time.Now()
			mail.SetReceiveTime(&now)
			attachment := maildao.GetAttachment(mail.ID)
			if len(attachment) > common.Zero {
				items := make([][]uint64, common.Zero)
				for _, val := range attachment {
					items = append(items, []uint64{val.ItemId, val.Num, val.SourceId})
				}
				event.Pub(gameevent.AddItemArray, gameevent.NewAddItemArrayEvent(roleId, items, true))
			}
			return true, nil
		}
	}
	return true, nil
}
func SetAllRead(ctx context.Context, setAllReadReq *maildto.AllReadReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	roleMail := maildao.GetRoleMailBy(roleId)
	for _, mail := range roleMail {
		if mail.IsRead {
			continue
		}
		mail.SetIsRead(true)
	}
	return true, nil
}
func SetAllReceived(ctx context.Context, req *maildto.AllReceiveMailReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	roleMail := maildao.GetRoleMailBy(roleId)
	for _, mail := range roleMail {
		if mail.IsReceived {
			continue
		}
		mail.SetIsReceived(true)
		now := time.Now()
		mail.SetReceiveTime(&now)
		attachment := maildao.GetAttachment(mail.ID)
		if len(attachment) > common.Zero {
			items := make([][]uint64, common.Zero)
			for _, val := range attachment {
				items = append(items, []uint64{val.ItemId, val.Num, val.SourceId})
			}
			event.Pub(gameevent.AddItemArray, gameevent.NewAddItemArrayEvent(roleId, items, true))
		}
	}
	return true, nil
}
func DeleteMail(ctx context.Context, deleteMailReq *maildto.DelMailReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	roleMail := maildao.GetRoleMailBy(roleId)
	for _, mail := range roleMail {
		if mail.ID == deleteMailReq.MailId {
			mail.SetIsDeleted(true)
			maildao.FlushRoleMail(roleId, roleMail)
		}
	}
}
